package com.gaolang.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaolang.web.entities.Health;
import com.gaolang.web.handler.BusinessException;
import com.gaolang.web.mapper.HealthMapper;
import com.gaolang.web.response.CustomizeResultCode;
import com.gaolang.web.response.ResultCode;
import com.gaolang.web.service.IHealthService;
import com.gaolang.web.vo.HealthVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gaolang
 * @since 2021-03-13
 */
@Service
public class HealthServiceImpl extends ServiceImpl<HealthMapper, Health> implements IHealthService {

    @Autowired
    private HealthMapper healthMapper;

    @Override
    public void report(HealthVO healthVO) {
        Health report = isReport(healthVO.getUserId());
        if(report!=null) {
            throw new BusinessException(ResultCode.ARITHMETIC_EXCEPTION.getCode(), "今日已经打卡,无法重复打卡！");
        }
        Health health = new Health();
        BeanUtils.copyProperties(healthVO,health);
        health.setCreateTime(new Date());
        healthMapper.insert(health);
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public Health isReport(Long id) {
        List<Health> health = healthMapper.isReport(id);
        if(health.size()>0){
            return  health.get(0);
        }
        return null;
    }
}
